var PrintOrderTemplate = function () {
  var template = /* html */ `
    <div class="print-order-template" style="padding:20px;padding-top:0;height:calc(100% - 20px)">
      <div style="margin-bottom:10px">
        <el-button type="primary" @click="print">打印</el-button>
        <template v-for="(searchBtn,index) in searchBtns">
          <span :key="'search-label_'+index" style="margin-left:10px;">{{searchBtn.label}}</span>
          <el-select filterable class="template-item" v-if="searchBtn.type==='select'" v-model="searchBtn.value" :key="'search-btn_'+index" 
            style="width:220px;" @change="searchChange(searchBtn)">
            <template v-for="(option,optionIndex) in searchBtn.options">
              <el-option v-if="typeof option==='object'" :key="'option_'+optionIndex" :value="option.value" :label="option.label"></el-option>
              <el-option v-else :key="'option_'+optionIndex" :value="option"></el-option>
            </template>
          </el-select>
        </template>
      </div>

      <div style="height:calc(100% - 50px);overflow:auto;" v-if="printTemplData!==null&&printTemplate!==null">
        <sys-print-template-order ref="printTemplateOrder" :print-data="printTemplData" :print-template="printTemplate" :read-only="readOnly">
        </sys-print-template-order>
      </div>
    </div>
  `;

  return {
    mixins: [printOrderTemplateMixin('outBusinessOrder')],

    data: function () {
      return {
        formatField:['outOrderType'],
        resetTemplField:'outOrderType'
      };
    },

    methods: {
      print: function () {
        this.toPrint();
      },

      getDropdownOption(option, printData) {
        var field = option.field;
        var options = [];
        var value = '';
        if (this.formatField.indexOf(field) !== -1) {
          option.nonSearch = true;
        }
        if (field === 'outOrderType') {
          options = [{
            value: 'electroplate',
            label: '电镀'
          }, {
            value: 'weld&polish',
            label: '焊接/打磨'
          }];

          var obj = _.find(printData, function (data) {
            var technology = data.technology;
            return technology.indexOf('电镀') !== -1;
          });
          value = obj !== undefined ? 'electroplate' : 'weld&polish';
        }
        return {
          options: options,
          value: value
        };
      },

      searchChange:function(searchBtn){
        if (searchBtn.nonSearch) {
          this.resetOrderTemplate(searchBtn);
          return;
        }
        this.searchTemplData();
      },

      setPrintData: function () {
        var searchData = _.clone(this.searchData);
        var createUserList = [];
        var approvalUserList = [];
        for (var i = 0; i < searchData.length; i++) {
          var data = searchData[i];
          data.unit = '付';
          var createUser = data.createUser;
          if (!SysUtil.isEmpty(createUser)) {
            createUserList.push(createUser);
          }

          var approvalUser = data.approvalUser;
          if (!SysUtil.isEmpty(approvalUser)) {
            approvalUserList.push(approvalUser);
          }
        }

        createUserList = _.uniq(createUserList);
        approvalUserList = _.uniq(approvalUserList);

        var tableHead = {
          createUserList: createUserList,
          approvalUserList: approvalUserList,
          produceTime: moment().valueOf()
        };

        var searchBtns = this.searchBtns;
        var searchBtn = _.find(searchBtns, {
          field: 'supplierName'
        });
        if (searchBtn !== undefined) {
          var supplierName = searchBtn.value;
          tableHead.supplierName = supplierName;
        }

        var data = {
          orderData: searchData,
          tableHead: tableHead
        };

        var self = this;
        SysUtil.getUser().then(function (user) {
          data.user = user;
          self.printTemplData = data;
        });
      },

      resetOrderTemplate:function(searchBtn){
        if (searchBtn === undefined) {
          this.printTemplate = this.orderTemplate;
          return;
        }
        var orderTemplate = _.cloneDeep(this.orderTemplate);
        var options = orderTemplate.options;
        var templType = searchBtn.value;
        for (var i = 0; i < options.length; i++) {
          var option = options[i];
          if (!option.isResetTempl) {
            continue;
          }
          var config = option.config;
          if (!_.isArray(config)) {
            continue;
          }
          option.config = config = _.find(config, {
            templateType:templType
          });
        }
        this.printTemplate = orderTemplate;
      }
    },

    template: template
  };
}();